package com.mall.hadoop.job.sellgarden;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.log4j.Logger;

import com.mall.manager.service.AreaMapService;
import com.mall.manager.service.SellStaticService;

/**
 * 分析园所统计结果后插入rds数据库
 * @author tgy
 *
 */
public class SellGardenMap extends Mapper<LongWritable, Text, Text, IntWritable>{
	
	Logger logger = Logger.getLogger(SellGardenMap.class);
	
	@Override
	protected void map(LongWritable key, Text value,Context context)throws IOException, InterruptedException {
		try{
			Map<String,Object> map = new HashMap<String,Object>();
			String[] str = value.toString().split(",");
			String name = null;
			//key包含p字符表示该行为省数据
			if(str[0].contains("p")){
				name = str[0].substring(0, str[0].length()-1);
				map = AreaMapService.loadAreaMap("p");
			}else if(str[0].contains("c")){  //key包含c字符表示该行为市数据
				name = str[0].substring(0, str[0].length()-1);
				map = AreaMapService.loadAreaMap("c");
			}else{  //表示该行为区数据
				name = str[0].substring(0, str[0].length()-1);
				map = AreaMapService.loadAreaMap("a");
			}
			SellStaticService.insertBatch(name, Integer.parseInt(str[1]),map);
		}catch(Exception e){
			logger.info("SellGardenMap园所数据存储异常："+e.toString());
		}
	}
	
}




